package com.polaris.live.ui.party.utils

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ScreenUtils
import com.bumptech.glide.Glide
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.util.StructUtils
import com.polaris.live.databinding.LayoutContributionAvaterViewTop1Binding
import com.polaris.live.databinding.LayoutContributionAvaterViewTop2Binding
import com.polaris.live.databinding.LayoutContributionAvaterViewTop3Binding
import com.polaris.live.databinding.LayoutLiveEntryBigBrotherBinding
import com.polaris.live.im.sfs.bean.live.SendLiveMessage
import com.polaris.live.im.sfs.bean.live.ext.MessageUserExtBean
import com.polaris.live.im.sfs.bean.live.payload.ContributionBarragePayloadDto
import com.polaris.live.utils.image.loadAvatar
import com.polaris.live.widget.avatar.FrameImageUtils
import kotlin.random.Random

/**
 * 大哥进场飘屏
 *
 * @author Created by 天晴 on 2024/5/28 14:08
 * @since 1.0.0
 **/
class BigBrotherEntersTheArenaProxy(private val group: ViewGroup, lifecycleOwner: LifecycleOwner) {

    private val screenWidth by lazy {
        ScreenUtils.getScreenWidth()
    }

    private val list = StructUtils.newSaleSet<ObjectAnimator>()

    init {
        lifecycleOwner.lifecycle.addObserver(object : LifecycleEventObserver {
            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                if (event == Lifecycle.Event.ON_DESTROY) {
                    list.forEach {
                        it.cancel()
                    }
                    list.clear()
                    group.removeAllViews()
                }
            }
        })
    }

    fun addData(sendLiveMessage: SendLiveMessage) {
        val mBinding = LayoutLiveEntryBigBrotherBinding.inflate(LayoutInflater.from(group.context), null, false)
        val index = group.indexOfChild(mBinding.root)
        if (index < 0) {
            group.addView(mBinding.root)
        }
        mBinding.contextTv.text = sendLiveMessage.content

        val ext = sendLiveMessage.ext as MessageUserExtBean
        val payload = sendLiveMessage.payload as ContributionBarragePayloadDto

        when (payload.rank) {
            1 -> {
                val top1Binding =
                    LayoutContributionAvaterViewTop1Binding.inflate(LayoutInflater.from(group.context), null, false)
                Glide.with(mBinding.root.context).loadAvatar(ext.user.avatar).into(top1Binding.avatarTop)
                mBinding.avRl.addView(top1Binding.root)
                mBinding.avFv.visibility = View.GONE
            }

            2 -> {
                val top2Binding =
                    LayoutContributionAvaterViewTop2Binding.inflate(LayoutInflater.from(group.context), null, false)
                Glide.with(mBinding.root.context).loadAvatar(ext.user.avatar).into(top2Binding.avatarTop)
                mBinding.avRl.addView(top2Binding.root)
                mBinding.avFv.visibility = View.GONE
            }

            3 -> {
                val top3Binding =
                    LayoutContributionAvaterViewTop3Binding.inflate(LayoutInflater.from(group.context), null, false)
                Glide.with(mBinding.root.context).loadAvatar(ext.user.avatar).into(top3Binding.avatarTop)
                mBinding.avRl.addView(top3Binding.root)
                mBinding.avFv.visibility = View.GONE
            }

            else -> {
                mBinding.avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(ext.user.avatarFrame))
                mBinding.avatar.setImageUrl(ext.user.avatar)
            }
        }

        val randomNumber = Random.nextInt(400, 601).autoDp
        val lp = FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        lp.topMargin = randomNumber
        mBinding.root.layoutParams = lp
        startAnm(mBinding.root)
    }

    private fun startAnm(view: View) {
        val animatorCenterX =
            ObjectAnimator.ofFloat(view, "translationX", screenWidth.toFloat(), -screenWidth.toFloat())
        animatorCenterX.duration = 8000
        animatorCenterX.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                group.post {
                    group.removeView(view)
                }
                list.remove(animatorCenterX)
            }
        })

        list.add(animatorCenterX)
        animatorCenterX.start()
    }
}